package com.turing.post.salesmanage.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.salesmanage.util.SaleOrderSalesDetail;
import com.turing.post.salesmanage.util.SaleOrderSalesDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * @author zxp
 * @Dec 23, 2012
 */

@Stateless(mappedName = "salesmanage.impl.SaleOrderSalesDetailDaoImpl")
@Remote
@Local
public class SaleOrderSalesDetailDaoImpl implements SaleOrderSalesDetailDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public SaleOrderSalesDetail getSaleOrderSalesDetailByPrimaryKey(
			SaleOrderSalesDetail saleOrderSalesDetail) {
		if (saleOrderSalesDetail.getSalesId() == null
				|| saleOrderSalesDetail.getSaleOrderId() == null) {
			return null;
		}
		return _em.find(SaleOrderSalesDetail.class, saleOrderSalesDetail);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SaleOrderSalesDetail> getSaleOrderSalesDetailListBySaleOrderId(
			BigInteger saleOrderId) {
		String sql = "select * from SaleOrderSalesDetail t where t.SaleOrderId=?1";
		Query query = _em.createNativeQuery(sql, SaleOrderSalesDetail.class)
				.setParameter(1, saleOrderId);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SaleOrderSalesDetail> getSaleOrderSalesDetailListBySaleOrderIdAndPage(
			BigInteger saleOrderId, Integer page) {
		String sql = "select * from SaleOrderSalesDetail t where t.SaleOrderId=?1";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, SaleOrderSalesDetail.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, saleOrderId);
		return query.getResultList();
	}

	@Override
	public BigInteger getSaleOrderSalesDetailTotalBySaleOrderId(
			BigInteger saleOrderId) {
		String sql = "select nvl(count(*),0) count from SaleOrderSalesDetail t where t.SaleOrderId=?1";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, saleOrderId);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insertSaleOrderSalesDetail(
			SaleOrderSalesDetail saleOrderSalesDetail)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, saleOrderSalesDetail);
	}

	@Override
	public void updateSaleOrderSalesDetail(
			SaleOrderSalesDetail saleOrderSalesDetail)
			throws PCPSApplicationException {
		GeneralDao.update(_em, saleOrderSalesDetail);
	}

	@Override
	public Integer deleteSaleOrderSalesDetail(
			SaleOrderSalesDetail saleOrderSalesDetail)
			throws PCPSApplicationException {
		String sql = "delete from SaleOrderSalesDetail t where t.SaleOrderId=?1 and SalesId=?2";
		try {
			Query query = _em.createNativeQuery(sql);
			query.setParameter(1, saleOrderSalesDetail.getSaleOrderId());
			query.setParameter(2, saleOrderSalesDetail.getSalesId());
			return query.executeUpdate();
		} catch (Exception e) {
			return -1;
		}
	}
}
